<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/svg/logo.svg" class="logo-style">
                <span>ANJO后台管理系统</span>
            </div>
            <el-dropdown trigger="click" @command="handleCommand">
                <p style="color: #dddddd">
                    设置<i class="el-icon-caret-bottom"></i>
                </p>
                <el-dropdown-menu slot="dropdown">
                <!-- <el-dropdown-item>公告栏</el-dropdown-item> -->
                <el-dropdown-item divided  command="myInfo">个人信息</el-dropdown-item>
                <el-dropdown-item divided  command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </el-header>
       <el-container>
           <!-- 侧边栏 -->
           <el-aside :width="isCollapse ? '64px' : '180px'">
               <div class="toggle-button" @click="toggleCollapse">
                   |||
               </div>
                <el-menu background-color="#373d44" text-color="#fff" active-text-color="#409EFF" unique-opened
                 :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
                    <el-menu-item index="/home">
                        <!-- 图标 -->
                        <i class="el-icon-s-home"/>
                        <span>首页</span>
                    </el-menu-item>
                    <el-submenu index="1" v-if="permissionStrValue.indexOf('/login-log') !== -1 
                    || permissionStrValue.indexOf('/data')  !== -1">
                        <template slot="title">
                            <i class="el-icon-date"/>
                            <span>基础数据</span>
                        </template>
                        <el-menu-item index="/login-log" @click="savePath('/login-log')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>登录日志</span>
                            </template>
                        </el-menu-item>
                        <el-menu-item index="/data" @click="savePath('/data')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>数据字典</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                    <el-submenu index="2" v-if="permissionStrValue.indexOf('/estate') !== -1 
                    || permissionStrValue.indexOf('/house-list')  !== -1 || permissionStrValue.indexOf('/house-request')  !== -1 
                    || permissionStrValue.indexOf('/rent-house')  !== -1 || permissionStrValue.indexOf('/add-house')  !== -1">
                        <template slot="title">
                            <i class="el-icon-office-building"/>
                            <span>房源管理</span>
                        </template>
                        <el-menu-item index="/estate" @click="savePath('/estate')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>楼盘列表</span>
                            </template>
                        </el-menu-item>
                        <el-menu-item index="/house-list" @click="savePath('/house-list')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>房源列表</span>
                            </template>
                        </el-menu-item>
                        <el-menu-item index="/house-request" @click="savePath('/house-request')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>看房请求</span>
                            </template>
                        </el-menu-item>
                        <el-menu-item index="/rent-house" @click="savePath('/rent-house')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>租房管理</span>
                            </template>
                        </el-menu-item>
                        <el-menu-item index="/add-house" @click="savePath('/add-house')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>新增房源</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                    <el-submenu index="3" v-if="permissionStrValue.indexOf('/house-host')  !== -1">
                        <template slot="title">
                            <i class="el-icon-receiving"/>
                            <span>房东管理</span>
                        </template>
                        <el-menu-item index="/house-host" @click="savePath('/house-host')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>房东列表</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                     <el-submenu index="5" v-if="permissionStrValue.indexOf('/promise-page')  !== -1">
                        <template slot="title">
                            <i class="el-icon-files"/>
                            <span>合约管理</span>
                        </template>
                        <el-menu-item index="/promise-page" @click="savePath('/promise-page')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>合同管理</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                     <el-submenu index="6" v-if="permissionStrValue.indexOf('/message')  !== -1 
                     || permissionStrValue.indexOf('/advertist')  !== -1">
                        <template slot="title">
                            <i class="el-icon-message"/>
                            <span>资讯管理</span>
                        </template>
                        <el-menu-item index="/message" @click="savePath('/message')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>广告列表</span>
                            </template>
                        </el-menu-item>
                        <el-menu-item index="/advertist" @click="savePath('/advertist')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>资讯列表</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                    <el-submenu index="7" v-if="permissionStrValue.indexOf('/question')  !== -1 ">
                        <template slot="title">
                            <i class="el-icon-edit-outline"/>
                            <span>问答管理</span>
                        </template>
                        <el-menu-item index="/question" @click="savePath('/question')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>问答列表</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                     <el-submenu index="9" v-if="permissionStrValue.indexOf('/interface-list')  !== -1 ">
                        <template slot="title">
                            <i class="el-icon-bell"/>
                            <span>性能检测</span>
                        </template>
                        <el-menu-item index="/interface-list" @click="savePath('/interface-list')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>接口压力</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                     <el-submenu index="10" v-if="permissionStrValue.indexOf('/task-list')  !== -1 ">
                        <template slot="title">
                            <i class="el-icon-date"/>
                            <span>定时任务</span>
                        </template>
                        <el-menu-item index="/task-list" @click="savePath('/task-list')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>任务列表</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                    <el-submenu index="11" v-if="permissionStrValue.indexOf('/userList')  !== -1 || permissionStrValue.indexOf('/roleList') !== -1
                       || permissionStrValue.indexOf('/permission-list')  !== -1  ">
                        <template slot="title">
                            <i class="el-icon-setting"/>
                            <span>系统管理</span>
                        </template>
                        <el-menu-item index="/userList" @click="savePath('/userList')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>用户列表</span>
                            </template>
                        </el-menu-item>
                        <el-menu-item index="/roleList" @click="savePath('/roleList')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>角色管理</span>
                            </template>
                        </el-menu-item>
                        <el-menu-item index="/permission-list" @click="savePath('/permission-list')">
                            <template slot="title">
                                <i class="el-icon-menu"/>
                                <span>权限管理</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
           </el-aside> 


           <!--右侧主体内容-->
            <el-main>
                <router-view></router-view>
            </el-main>
           
       </el-container>

       <div>
            <el-dialog title="提示" :visible.sync="show" width="30%">
                <el-form :model= "userInfo" label-width="100px" :rules="formRules" ref="userInfoForm">
                <el-form-item label="身份证：" prop="identityId">
                    <el-input size="small" v-model="userInfo.identityId"></el-input>
                </el-form-item>
                <el-form-item label="姓名：" prop="username">
                    <el-input size="small" v-model="userInfo.username"></el-input>
                </el-form-item>
                <el-form-item label="联系电话：" prop="phone">
                    <el-input size="small" v-model="userInfo.phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱：" prop="email">
                    <el-input size="small" v-model="userInfo.email"></el-input>
                </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="show = false">取 消</el-button>
                    <el-button type="primary" @click="updateUserInfo">修改</el-button>
               </span>
            </el-dialog>
        </div>
    </el-container>
    
</template>

<script>
export default {
    name: "Main",
    data() {
         return {
            //是否进行菜单栏折叠,false代表不折叠
            isCollapse: false,
            //被激活的连接地址
            activePath: '',
            dialogVisible: false,
            show: false,
            userInfo: {
                id: '',
                email: '',
                phone: '',
                state: '',
                username: '',
                identityId: '',
                createTime: '',
                avatar: '',
                permissionStrValue: '',
                permissionList: ''
            
            },
            formRules:{
                username: [
                    {required: true, message: '请输入用户名', trigger: 'blur'}
                ],
                phone: [
                    {required: true, message: '请输入电话号码', trigger: 'blur'}
                ],
                identityId: [
                     {required: true, message: '请输入身份证', trigger: 'blur'}
                ],
                email: [
                     {required: true, message: '请输入邮箱', trigger: 'blur'}
                ]

           }
         }

    },
    created() {
       //为了设置当前按钮的被点击时，显示高亮
       this.activePath = window.sessionStorage.getItem('activePath');
       //获取权限值
       this.permissionStrValue = JSON.parse(window.sessionStorage.getItem('permissionList')).toString();
       console.log("权限：" + this.permissionStrValue)
       
    },
    methods: {
        handleCommand(tag) {
            if(tag === 'logout') {
                this.logout();
            }
            if (tag == 'myInfo'){
                var user = JSON.parse(window.sessionStorage.getItem('user'));
                this.userInfo.id = user.id;
                this.userInfo.username = user.username;
                this.userInfo.email = user.email;
                this.userInfo.phone = user.phone;
                this.userInfo.state = user.state;
                this.userInfo.identityId = user.identityId;
                this.userInfo.createTime = user.createTime;
                this.userInfo.avatar = user.avatar;
                this.show = true;
            }
        },
        myInfo() {

        },
        logout() {
            //把本地的token清空
            window.sessionStorage.clear();
            //路由到登录页
            this.$router.push('/login')

        },
        toggleCollapse() {
           this.isCollapse = !this.isCollapse;
        },
        savePath(url) {
            window.sessionStorage.setItem('activePath', url);
            this.activePath = url;
        },
        async updateUserInfo() {
            this.$refs.userInfoForm.validate(async valid =>{
                if (valid){
                    const {data: res} = await this.$http.post('/user/member/update-member-info',JSON.stringify(this.userInfo));
                    if (!res.success){
                    return this.$message.error(res.msg);
                    }
                    this.dialogVisible = false;
                    return this.$message.success("更改成功!");
                }else {
                    return this.$message.error("请填写完整表单！");
                }
                })
           }

        }
}
</script>

<style scoped lang="less">
    .home-container{
    height: 100%;
  }
  .el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #ffffff;
    font-size: 20px;
    > div{
      display: flex;
      align-items: center;
      span{
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #373d41;
    .el-menu{
      border-right: none;
    }

  }

  .el-main {
    background-color: #E9EEF3;
  }
  .logo-style{
    height: 60px;
    width: 40px;
    margin-left: 15px;
  }

  .toggle-button{
    background-color: #373d41;
    font-size: 10px;
    line-height: 24px;
    color: #ffffff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }

</style>